package com.ctvit.cch.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctvit.cch.core.mapper.SocialPlatformMapper;
import com.ctvit.cch.core.pojo.domain.SocialPlatform;
import com.ctvit.cch.core.pojo.dto.SocialPlatformAddDTO;
import com.ctvit.cch.core.pojo.dto.SocialPlatformEditDTO;
import com.ctvit.cch.core.pojo.dto.SocialPlatformListDTO;
import com.ctvit.cch.core.pojo.dto.SocialPlatformStatusDTO;
import com.ctvit.cch.core.pojo.vo.SocialPlatformGetVO;
import com.ctvit.cch.core.pojo.vo.SocialPlatformListVO;
import com.ctvit.cch.core.pojo.vo.SocialPlatformSelectVO;
import com.ctvit.cch.core.service.ISocialPlatformService;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 社交平台Service业务层处理
 *
 * @author heyingcheng
 * @date 2025/07/23
 */
@Slf4j
@Service
public class SocialPlatformServiceImpl extends ServiceImpl<SocialPlatformMapper, SocialPlatform> implements ISocialPlatformService {

    @Override
    public TableDataInfo list(SocialPlatformListDTO dto) {
        LambdaQueryWrapper<SocialPlatform> qWrapper = new LambdaQueryWrapper<>();
        qWrapper.eq(StringUtils.isNotBlank(dto.getPlatformId()), SocialPlatform::getPlatformId, dto.getPlatformId());
        qWrapper.like(StringUtils.isNotBlank(dto.getPlatformName()), SocialPlatform::getPlatformName, dto.getPlatformName());
        qWrapper.eq(Objects.nonNull(dto.getPlatformType()), SocialPlatform::getPlatformType, dto.getPlatformType());
        qWrapper.eq(StringUtils.isNotBlank(dto.getStatus()), SocialPlatform::getStatus, dto.getStatus());
        qWrapper.like(StringUtils.isNotBlank(dto.getPlatformDesc()), SocialPlatform::getPlatformDesc, dto.getPlatformDesc());
        qWrapper.orderByAsc(SocialPlatform::getOrderNum);

        Page<SocialPlatform> page = page(new Page<>(dto.getPageNum(), dto.getPageSize()), qWrapper);

        List<SocialPlatformListVO> records = page.getRecords().stream().map(t -> {
            SocialPlatformListVO v = new SocialPlatformListVO();
            BeanUtils.copyProperties(t, v);
            return v;
        }).collect(Collectors.toList());

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(records);
        tableDataInfo.setTotal(page.getTotal());

        return tableDataInfo;
    }

    @Override
    public boolean add(SocialPlatformAddDTO dto) {
        SocialPlatform entity = new SocialPlatform();
        BeanUtils.copyProperties(dto, entity);
        entity.setPlatformId(IdWorker.getIdStr());
        return save(entity);
    }

    @Override
    public boolean edit(SocialPlatformEditDTO dto) {
        SocialPlatform entity = new SocialPlatform();
        BeanUtils.copyProperties(dto, entity);
        return updateById(entity);
    }

    @Override
    public SocialPlatformGetVO get(String id) {
        SocialPlatformGetVO target = new SocialPlatformGetVO();
        BeanUtils.copyProperties(getById(id), target);
        return target;
    }

    @Override
    public List<SocialPlatformSelectVO> select() {
        LambdaQueryWrapper<SocialPlatform> qWrapper = new LambdaQueryWrapper<>();
        qWrapper.select(SocialPlatform::getPlatformId, SocialPlatform::getPlatformName);
        return list(qWrapper).stream().map(t -> {
            SocialPlatformSelectVO v = new SocialPlatformSelectVO();
            BeanUtils.copyProperties(t, v);
            return v;
        }).collect(Collectors.toList());
    }

    @Override
    public boolean status(SocialPlatformStatusDTO dto) {
        LambdaUpdateWrapper<SocialPlatform> uWrapper = new LambdaUpdateWrapper<>();
        uWrapper.eq(SocialPlatform::getPlatformId, dto.getPlatformId());
        uWrapper.set(SocialPlatform::getStatus, dto.getStatus());
        uWrapper.set(SocialPlatform::getUpdateBy, SecurityUtils.getUsername());
        uWrapper.set(SocialPlatform::getUpdateTime, LocalDateTime.now());
        return update(uWrapper);
    }

}
